package com.scau.database.advice;

import com.scau.database.entity.aftersale.ResultVO;
import com.scau.database.enums.ResultCode;
import com.scau.database.exception.APIException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.xml.transform.Result;

/**
 * @ClassName: ExceptionControllerAdvice
 * @Description: TODO
 * @Author: sunslikes
 * @Date: 2020/6/26 2:59
 * @Version: 1.0
 */
@RestControllerAdvice(basePackages = {"com.scau.database.controller.aftersalesstaff","com.scau.database.controller.user"})
public class ExceptionControllerAdvice {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultVO<String> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        // 从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        // 然后提取错误提示信息进行返回
        return new ResultVO<>(ResultCode.VALIDATE_FAILED, objectError.getDefaultMessage());
    }

    @ExceptionHandler(APIException.class)
    public ResultVO<String> APIExceptionHandler(APIException e) {
        return new ResultVO<>(ResultCode.FAILED, e.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public ResultVO<String> ExceptionHandler(Exception e) {
        e.printStackTrace();
        return new ResultVO<>(ResultCode.ERROR, "出问题了");
    }

}
